// pages/addaddress/addaddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressId:0,
        region:['省','市','区'],
        city:'',
        receiver:'',
        phone:'12312312312',
        area:'xxxx街道'
        
    },
   
    

    return: function(e){
        wx.navigateBack({data:1})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    getAddressDetail(){
        wx.get('http://localhost:3000/wx/address/detail',{
            addressId:this.data.addressId
        }).then(res => {
            this.setData({
                receiver:res.receiver,
                phone:res.phone,
                city:res.city,
                area:res.area
            })
        })
    },
    onLoad(options) {
        if(options.addressId){
            this.setData({
                addressId:options.addressId
            })
            this.getAddressDetail()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    back1:function(){//地址管理
        wx.navigateTo({
          url: '/pages/address/address'
        })
    },
    bindRegionChange: function(e){
        console.log('pick', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    }, 
    fromName:function(e){
        this.setData({
            receiver : e.detail.value
        })
    },
    fromPhone:function(e){
        this.setData({
            phone : e.detail.value
        })
    },
    fromArea:function(e){
        this.setData({
            area : e.detail.value
        })
    },
    sumbit(){
        let address = {
            memberId:wx.userInfo._id,
            receiver:this.data.receiver,
            phone:this.data.phone,
            area:this.data.area,
            city:this.data.region.join('')
        }
        //收货地址的校验
        if(!address.receiver || !/^[\u2E80-\u9FFF]+$/.test(address.receiver)){
            wx.showToast({
              title: '请输入正确的收货人',
              icon:'none',
              duration:2000
            })
            return false
        }
        if(!address.phone || !/^1\d{10}$/.test(address.phone)){
            wx.showToast({
              title: '请输入正确的号码',
              icon:'none',
              duration:2000
            })
            return false
        }
        if(!address.city && !this.data.addressId){
            wx.showToast({
              title: '请选择地址',
              icon:'none',
              duration:2000
            })
            return false
        }

        if(this.data.addressId){//修改
            address.addressId = this.data.addressId
            wx.put('http://localhost:3000/wx/address',address).then(res => {
                if(!res.errorMsg){
                    wx.showToast({
                    title: '修改成功',
                    icon:'success',
                    duration:2000
                    })
                    setTimeout(()=>{   
                        wx.navigateBack({
                        delta: -1,
                        })},2000)
                }else{
                    wx.showToast({
                    title: res.errorMsg,
                    icon:'none',
                    duration:2000
                    })
                    setTimeout(()=>{   
                        wx.navigateBack({
                        delta: -1,
                        })},2000)
                    
                }
            })
    }else{//新增
        wx.post('http://localhost:3000/wx/address',address).then(res => {
                    if(!res.errorMsg){
                        wx.showToast({
                        title: '新增成功',
                        icon:'success',
                        duration:2000
                        })
                        setTimeout(()=>{   
                            wx.navigateBack({
                            delta: -1,
                            })},2000)
                    }else{
                        wx.showToast({
                        title: res.errorMsg,
                        icon:'none',
                        duration:2000
                        })
                        setTimeout(()=>{   
                            wx.navigateBack({
                            delta: -1,
                            })},2000)
                        
                    }
                })
        }

      
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})